﻿
using Elsa.Workflows;
using Elsa.Workflows.Models;
using Elsa.Workflows.Options;
using Elsa.Workflows.Runtime;
using Elsa.Workflows.Runtime.Messages;

namespace ElsaDemo
{
    public class TestHostedService : BackgroundService
    {
        private readonly IWorkflowClient _workflowClient;
        private readonly IServiceScopeFactory _scopeFactory;

        public TestHostedService(IServiceScopeFactory scopeFactory)
        {
            _scopeFactory = scopeFactory;
            //_workflowClient = workflowClient;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var workflowDefinitionId = "Workflow02";
                //var runner = scope.ServiceProvider.GetRequiredService<IWorkflowRunner>();
                //var result = await runner.RunAsync<Workflow2>(null, stoppingToken);
                var workflowRuntime = scope.ServiceProvider.GetRequiredService<IWorkflowRuntime>();
                var client = await workflowRuntime.CreateClientAsync();
                var result = await client.CreateAndRunInstanceAsync(new CreateAndRunWorkflowInstanceRequest { WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(workflowDefinitionId), CorrelationId = Guid.NewGuid().ToString() });
                var tt = "";
            }
            
        }
    }
}
